home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / waitfor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  2.0 KB  |  134 lines

  1. /*
  2.  * Wait for a string on the stdin.  Returns a 0 on success, 1 on failure
  3.  * and -1 on error.  This is an external program designed to be used in
  4.  * shell scripts.
  5.  */
  6.  
  7. #define TIMEOUT    10
  8. #define BUF_SIZ    1024
  9. #define STRSTR
  10.  
  11. int wf_flag;
  12.  
  13. #include <stdio.h>
  14. #include <signal.h>
  15. #include "config.h"
  16.  
  17. #ifdef BSD
  18. #include <setjmp.h>
  19. jmp_buf wf_buf;
  20. #endif /* BSD */
  21.  
  22. main(argc, argv)
  23. int argc;
  24. char *argv[];
  25. {
  26.     int i, j, timeout;
  27.     char c, buf[BUF_SIZ], *string, *strstr();
  28.     long t, time();
  29.     void exit();
  30.  
  31.     if (argc < 2 || argc > 3) {
  32.         fprintf(stderr, "Usage: waitfor -n string\n");
  33.         exit(-1);
  34.     }
  35.  
  36.     if (argv[1][0] == '-') {
  37.         timeout = atoi(&argv[1][1]);
  38.         if (argc != 3) {
  39.             fprintf(stderr, "Usage: waitfor -n string\n");
  40.             exit(-1);
  41.         }
  42.         string = argv[2];
  43.     }
  44.     else {
  45.         timeout = TIMEOUT;
  46.         string = argv[1];
  47.     }
  48.                     /* here we go.. */
  49.     i = 0;
  50.     time(&t);
  51.     while ((time((long *) 0) - t) < timeout) {
  52.         if ((j = getc_line()) != -1) {
  53.             c = j & 0x7f;
  54.                     /* no NULLs please */
  55.             if (c != '\0') {
  56.                 buf[i++] = c;
  57.                 buf[i] = '\0';
  58.             }
  59.  
  60.             if (i >= BUF_SIZ) {
  61.                 fprintf(stderr, "waitfor: buffer overflow\n");
  62.                 exit(-1);
  63.             }
  64.                     /* yea.. we found it! */
  65.             if (strstr(buf, string))
  66.                 exit(0);
  67.         }
  68.     }
  69.     exit(1);
  70. }
  71.  
  72. int
  73. getc_line()
  74. {
  75.     int wf_force();
  76.     char c;
  77.     unsigned int alarm();
  78.  
  79.     signal(SIGALRM, (SIG_TYPE(*) ()) wf_force);
  80.     wf_flag = 0;
  81.  
  82.     alarm(1);
  83.  
  84. #ifdef BSD
  85.     if (setjmp(wf_buf))
  86.         return(-1);
  87. #endif /* BSD */
  88.  
  89.     if (read(0, &c, 1) <= 0) {
  90.         alarm(0);
  91.         return(-1);
  92.     }
  93.     if (wf_flag)
  94.         return(-1);
  95.     alarm(0);
  96.     return(c & 0xff);
  97. }
  98.  
  99. /* ARGSUSED */
  100. int
  101. wf_force(dummy)
  102. int dummy;
  103. {
  104. #ifdef BSD
  105.     longjmp(wf_buf, 1);
  106. #else /* BSD */
  107.     signal(SIGALRM, (SIG_TYPE(*) ()) wf_force);
  108.     wf_flag = 1;
  109. #endif /* BSD */
  110. }
  111.  
  112. #ifdef STRSTR
  113. /*
  114.  * Return a pointer to the first occurrence of string str2 in str1.
  115.  * Returns a NULL if str2 is not in str1.
  116.  */
  117.  
  118. char *
  119. strstr(str1, str2)
  120. char *str1, *str2;
  121. {
  122.     int len;
  123.     len = strlen(str2);
  124.     while (*str1) {
  125.         if (*str2 == *str1) {
  126.             if (!strncmp(str2, str1, len))
  127.                 return(str1);
  128.         }
  129.         str1++;
  130.     }
  131.     return(NULL);
  132. }
  133. #endif /* STRSTR */
  134.